/**
 * Created with Vim7.3 Mac
 * @fileOverview : 
 * @author : Lyle lylechen2014@gmail.com
 * @since : 2016-09-01 17:52:01
 * @filename : src/base/router/router.js
 * @version : 
 * @description : 
 */
define(function(require, exports, module){
     
    var tthis = this,   
        routes = {
           '/:module': function(module) {routerHandle(module,'normal','')},
           '/:module/:param': function(module,type) {routerHandle(module,type,'')},
           '/:module/:type/:param': function(module,type,param) {routerHandle(module,type,param)},
           '/.*': function(module) {tthis.routerHandle('error','page','')},
        };

    function routerHandle(module,type,param) {     
        obj = {
            name:module,
            type:type,
            param:param        
        };
        dispatcher(obj);
    }

    function dispatcher(obj) {

        if (obj.type !== 'normal') {
            //window.location.href = 'https://wwww'+siteName+'.com/'+obj.name+'.html'; 
        }

        require.async(getModulePath(obj.name),function(module){
            if (module) {
                module.render(obj); 
            } else {
                alert('Loading module failed. Please refresh and try again!');
            }
        });
    }

    function getModulePath(value) {
         
        var moduleName = value;

        if (typeof(Config.ABTest)!=='undefined' && Config.ABTest[value]) {
            value = Config.ABTest[value];
        }

        var modulePath = '{base}/pages/'+value;

        if (typeof(Config.moduleDiff)!=='undefined' && _.indexOf(Config.moduleDiff, moduleName) > -1) {
            modulePath = '{site}/pages/'+value;
        }

        console.log(modulePath);
  
        return modulePath;
    }

    module.exports = routes;
});

